//
//  RegisterController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/13.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "RegisterController.h"
static NSUInteger timerNum = 60;
static NSString * CodeStr;

@interface RegisterController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *UseriPhoneText;
@property (weak, nonatomic) IBOutlet UITextField *CordText;
@property (weak, nonatomic) IBOutlet UIButton *HuoQuBtn;
@property (weak, nonatomic) IBOutlet UILabel *TimeLabel;
@property (weak, nonatomic) IBOutlet UITextField *UserPWText;
@property (weak, nonatomic) IBOutlet UITextField *UserPWText01;
@property (weak, nonatomic) IBOutlet UITextField *OtheriPhoneText;

@property (weak, nonatomic) IBOutlet UIButton *RegisterBtn;
@property (strong, nonatomic) NSTimer * timer;

@end

@implementation RegisterController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    [self.timer invalidate];
    timerNum = 60;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.UseriPhoneText.keyboardType = UIKeyboardTypeNumberPad;
    self.CordText.keyboardType = UIKeyboardTypeNumberPad;
    self.UseriPhoneText.delegate = self;
    self.CordText.delegate = self;
    self.UserPWText.delegate = self;
    self.UserPWText.secureTextEntry = YES;
    self.UserPWText01.delegate = self;
    self.UserPWText01.secureTextEntry = YES;
    self.OtheriPhoneText.delegate = self;
    self.OtheriPhoneText.keyboardType = UIKeyboardTypeNumberPad;
    
    self.TimeLabel.layer.cornerRadius = self.TimeLabel.bounds.size.height/2;
    self.TimeLabel.layer.borderWidth = 1;
    self.TimeLabel.layer.borderColor = KRGBColor(247, 122, 39).CGColor;
    
    [self.RegisterBtn setImage:[UIImage imageNamed:@"LoginImage08_None"] forState:UIControlStateNormal];
    self.RegisterBtn.layer.cornerRadius = 18;
    self.RegisterBtn.layer.shadowColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.75].CGColor;
    self.RegisterBtn.layer.shadowOffset = CGSizeMake(0,4);
    self.RegisterBtn.layer.shadowOpacity = 1;
    self.RegisterBtn.layer.shadowRadius = 8;
    
}

- (IBAction)ClickOnBtnToDismiss:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)ClickOnBtnToHuoQuCord:(id)sender {
    
    if (![MainObjectWay IsMobileWithString:self.UseriPhoneText.text]) {
        [MBProgressHUD showAlertViewWithText:@"请输入正确的手机号"];
        return;
    }
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:self.UseriPhoneText.text forKey:@"phone"];
    
    [MainObjectWay PostWithMethod:@"send" andAction:@"Server" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"请求的信息 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            self.HuoQuBtn.hidden = YES;
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(test) userInfo:nil repeats:YES];
        }
        [MBProgressHUD showAlertViewWithText:responseObject[@"msg"]];
    } failure:^(NSError *error) {
        [MBProgressHUD showAlertViewWithText:@"获取失败，请稍后重试"];
        NSLog(@"错误信息 = %@",error);
    }];
}
- (void)test{
    
    if (timerNum == 0) {
        timerNum = 60;
        self.TimeLabel.text = [NSString stringWithFormat:@"获取验证码"];
        self.HuoQuBtn.hidden = NO;
        [self.timer invalidate];
        
    }else{
        
        timerNum = timerNum - 1;
        self.TimeLabel.text = [NSString stringWithFormat:@"%lds重新获取验证码",(long)timerNum];
    }
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (KIsiPhoneX) {
        return;
    }
    CGRect fram = self.view.frame;
    if (fram.origin.y == -110) {
        
    }else{
        [UIView animateWithDuration:0.3 animations:^{
            CGRect fram = self.view.frame;
            fram.origin.y = -110;
            self.view.frame = fram;
        }];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (self.UseriPhoneText.text.length > 0 && self.CordText.text.length > 0 && self.UserPWText.text.length > 0 && self.UserPWText01.text.length > 0 && self.OtheriPhoneText.text.length > 0) {
        
        [self.RegisterBtn setImage:[UIImage imageNamed:@"LoginImage08_Select"] forState:UIControlStateNormal];
        self.RegisterBtn.layer.shadowColor = [UIColor colorWithRed:63/255.0 green:203/255.0 blue:255/255.0 alpha:0.75].CGColor;
        
    }else{
        
        [self.RegisterBtn setImage:[UIImage imageNamed:@"LoginImage08_None"] forState:UIControlStateNormal];
        self.RegisterBtn.layer.shadowColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.75].CGColor;
    }
    if (KIsiPhoneX) {
        return;
    }
    [UIView animateWithDuration:0.3 animations:^{
        CGRect fram = self.view.frame;
        fram.origin.y = 0;
        self.view.frame = fram;
    }];
}

- (IBAction)ClickOnBtnToUpLoadUserNews:(id)sender {
    
    if (![MainObjectWay IsMobileWithString:self.UseriPhoneText.text]) {
        [MBProgressHUD showAlertViewWithText:@"请输入正确的手机号"];
        return;
    }
    
    if (self.CordText.text.length != 6) {
        [MBProgressHUD showAlertViewWithText:@"请输入六位数验证码"];
        return;
    }
    
    if (self.UserPWText.text.length < 6) {
        [MBProgressHUD showAlertViewWithText:@"请至少输入6位数密码"];
        return;
    }
    
    if (![self.UserPWText.text isEqual:self.UserPWText01.text]) {
        [MBProgressHUD showAlertViewWithText:@"两次密码不一致"];
        return;
    }
    
    if (![MainObjectWay IsMobileWithString:self.OtheriPhoneText.text]) {
        [MBProgressHUD showAlertViewWithText:@"请填写正确的推荐人手机号"];
        return;
    }
    
    NSMutableDictionary * dic01 = [[NSMutableDictionary alloc] init];
    [dic01 setObject:self.UseriPhoneText.text forKey:@"phone"];
    [dic01 setObject:self.UserPWText.text forKey:@"password"];
    [dic01 setObject:self.UserPWText01.text forKey:@"repassword"];
    [dic01 setObject:self.CordText.text forKey:@"smscode"];
    [dic01 setObject:self.OtheriPhoneText.text  forKey:@"parent"];
    
    [MainObjectWay PostWithMethod:@"index" andAction:@"Register" andAddParam:YES parameters:dic01 success:^(id responseObject) {
        NSLog(@"responseObject = %@",responseObject);
        NSLog(@"msg = %@",responseObject[@"msg"]);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        [MBProgressHUD showAlertViewWithText:responseObject[@"msg"]];
    } failure:^(NSError *error) {
        NSLog(@"error = %@",error);
        [MBProgressHUD showAlertViewWithText:@"注册失败"];
    }];
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
